package com.zeekr.sdk.storage;

import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * @author fqian
 * @date 2020/10/21
 */
public class ProcessUtils {
    public static final String TAG = "ProcessUtils";

    /**
     * 杀死自己这个进程
     */
    public static void killSelf() {
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 杀死可被发现的除主进程之外的所有进程
     *
     * @param context
     */
    public static void killAllOtherProcess(Context context) {
        if (context == null) {
            return;
        }
        //获取系统中所有正在运行的进程
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        //获取当前activity所在的进程
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        String currentProcess = context.getApplicationInfo().processName;
        //对系统中所有正在运行的进程进行迭代，如果进程名不是当前进程，则Kill掉       
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            String processName = appProcessInfo.processName;
            if (!processName.equals(currentProcess)) {
                activityManager.killBackgroundProcesses(processName);
            }

        }
    }

    /**
     * 兼容4.4 部分 手机判断错误的情况
     *
     * @param context
     * @return
     */
    public static boolean isMainProcess(Context context) {
        if (!TextUtils.equals(context.getPackageName(), getCurrentProcessName(context))) {
            return false;
        }
        return true;
    }

    private static String getCurrentProcessName(Context context) {
        // get by ams
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
        if (processes != null) {
            int pid = android.os.Process.myPid();
            for (ActivityManager.RunningAppProcessInfo processInfo : processes) {
                if (processInfo.pid == pid && !TextUtils.isEmpty(processInfo.processName)) {
                    return processInfo.processName;
                }
            }
        }

        // get from kernel
        String name = getProcessName(android.os.Process.myPid());
        if (!TextUtils.isEmpty(name) && name.contains(context.getPackageName())) {
            return name;
        }

        return null;
    }

    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
